package com.controller
{
	import com.model.valueObjects.VoMarkupHeader;
	import com.service.MarkupHeader;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class MarkupMasterController extends AbstractController
	{
		[Inject]
		public var markupMasterService:MarkupHeader;
		
		public function MarkupMasterController()
		{
			super();
		}
		[EventHandler(event="MarkupMasterEvent.GET_ALL")]
		public function getAll():void{
			model.currentAdminTableIndex=10;
			sh.executeServiceCall(markupMasterService.getAllMarkups() ,getAllResult, handleFault );
		}
		private function getAllResult(event:ResultEvent):void
		{
			model.markupMasterList=event.result as ArrayCollection;
			if(model.currentAdminTableIndex==10)
			{
				CursorManager.removeBusyCursor();
				model.busyCurrsorOn=true;	
			}
		}
		
		[EventHandler(event="MarkupMasterEvent.ADD", properties="markupMastervo")]
		public function add(markupMastervo:VoMarkupHeader):void
		{
			sh.executeServiceCall(markupMasterService.addNewMarkup(markupMastervo), addResult, handleFault );	
		}
		private function addResult(event:ResultEvent):void
		{
			model.markupMasterList=event.result as ArrayCollection;
			
			if(model.saveCloseBtn)model.currentForm=1;

			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("New Markup Added","Markup" );
		}
		
		[EventHandler(event="MarkupMasterEvent.UPDATE", properties="markupMastervo")]
		public function updateCompany(markupMastervo:VoMarkupHeader):void
		{
			sh.executeServiceCall(markupMasterService.editMarkup(markupMastervo), updateResult, handleFault );	
		}
		private function updateResult(event:ResultEvent):void
		{
			model.markupMasterList=event.result as ArrayCollection;
			
			model.currentForm=1;
			getAll();
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("Details Updated ","Markup");
		}
		
		[EventHandler(event="MarkupMasterEvent.DELETE", properties="serialNo")]
		public function deleteR(serialNo:int):void
		{
			sh.executeServiceCall(markupMasterService.deleteMarkup(serialNo), deleteResult, handleFault );	
		}
		private function deleteResult(event:ResultEvent):void
		{
			model.markupMasterList=event.result as ArrayCollection;
			model.currentList=model.markupMasterList;
			model.currentForm=1;
			getAll();
			CursorManager.removeBusyCursor();
			Alert.show("Record Deleted ","Markup" );
		}
		
		[EventHandler(event="MarkupMasterEvent.GETMMARKUP")]
		public function getMarginalMarkup():void{
			sh.executeServiceCall(markupMasterService.getMarginalMarkup(), getMarginalResult, handleFault );
		}
		private function getMarginalResult(event:ResultEvent):void{
			model.getMarginalMarkup=event.result as ArrayCollection;
		}
		
		[EventHandler(event="MarkupMasterEvent.GETNETMARKUP")]
		public function getNetMarkup():void{
			sh.executeServiceCall(markupMasterService.getNetMarkup(), getNetResult, handleFault );
		}
		private function getNetResult(event:ResultEvent):void{
			model.getNetMarkup=event.result as ArrayCollection;
		}
		
		private function handleFault(event:FaultEvent):void
		{
			Alert.show( event.fault.faultDetail, "Error" );
		}
	}
}